//
//  RootViewController2.m
//  真·豆瓣半完全体
//
//  Created by dllo on 15/12/31.
//  Copyright © 2015年 lanou3g. All rights reserved.
//

#import "RootViewController2.h"
#import "MyTableViewCell3-1.h"
#import "UIImageView+WebCache.h"
#import "ActivityDataBaseHandle.h"
#import "ActivityModel.h"
@interface RootViewController2 ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *arrData;

@property (nonatomic, retain) NSMutableDictionary *num;
@end

@implementation RootViewController2
- (void) dealloc{
    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = _title1;
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 375, 554) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    [_tableView release];
    
    UIImage *img = [UIImage imageNamed:@"btn_nav_share@2x.png"];
    img = [img imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIImage *img2 = [UIImage imageNamed:@"btn_nav_back@2x.png"];
    img2 = [img2 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithImage:img style:UIBarButtonItemStylePlain target:self action:@selector(itemAction:)];
    self.navigationItem.rightBarButtonItem = item;
    UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithImage:img2 style:UIBarButtonItemStylePlain target:self action:@selector(itemAction2:)];
    self.navigationItem.leftBarButtonItem = item2;
    
    [_tableView registerClass:[MyTableViewCell3_1 class] forCellReuseIdentifier:@"cell"];
    self.num = [NSMutableDictionary dictionary];

}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:17], NSFontAttributeName, nil];
    CGRect rect = [_content boundingRectWithSize:CGSizeMake(375, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return 260 + rect.size.height ;
}
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
     MyTableViewCell3_1 *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    cell.nameLabel.text = _name;
    [cell.imgView sd_setImageWithURL:[NSURL URLWithString:_img]];
    cell.imgView2.image = [UIImage imageNamed:@"icon_date_blue@2x.png"];
    cell.imgView3.image = [UIImage imageNamed:@"icon_sponsor_blue@2x.png"];
    cell.imgView5.image = [UIImage imageNamed:@"icon_spot_blue@2x.png"];
    cell.imgView4.image = [UIImage imageNamed:@"icon_catalog_blue@2x.png"];
    cell.Label.text = @"活动介绍";
    cell.contentLabel.text = _content;
    cell.contentLabel.numberOfLines = 20;
    cell.timeLabel.text = _time;
    cell.owner_nameLabel.text = _owner_name;
    cell.category_nameLabel.text =  _category_name;
    cell.addressLabel.text = _address;
    cell.addressLabel.numberOfLines = 2;
    
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;  //选中cell时不变色
   
    
    
    return cell;
}
- (void) itemAction:(UIBarButtonItem *) item{
    ActivityModel *model = [ActivityModel activityModel];
    model.title = _name;
    if ([[ActivityDataBaseHandle sharedActivityDataBaseHandle] selectModle:model] == YES) {
                    UIAlertController *alert2 = [UIAlertController alertControllerWithTitle:@"提示" message:@"该活动已收藏" preferredStyle:UIAlertControllerStyleAlert];
                    UIAlertAction *alertAction2 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];
                    [alert2 addAction:alertAction2];
                    [self presentViewController:alert2 animated:YES completion:^{
        
                    }];
    }
    else{
                UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"收藏成功" preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];
                [alert addAction:alertAction];
                [self presentViewController:alert animated:YES completion:^{
        
                }];
                [_num setObject:@"已收藏" forKey:_name];
                    NSLog(@"%@", _num);
                    NSLog(@"%@", _Num2);
                self.block(_name);
                    ActivityModel *model = [ActivityModel activityModel];
                    model.title = _name;
                    [[ActivityDataBaseHandle sharedActivityDataBaseHandle] insertModel:model];
    }
}

//- (void) itemAction:(UIBarButtonItem *) item{
//    if (![[_num objectForKey:_name ] isEqualToString:@"已收藏"]) {
//        if (![[_Num2 objectForKey:_name ] isEqualToString:@"已收藏"]) {
//        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"收藏成功" preferredStyle:UIAlertControllerStyleAlert];
//        UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];
//        [alert addAction:alertAction];
//        [self presentViewController:alert animated:YES completion:^{
//            
//        }];
//        [_num setObject:@"已收藏" forKey:_name];
//            NSLog(@"%@", _num);
//            NSLog(@"%@", _Num2);
//        self.block(_name);
//            ActivityModel *model = [ActivityModel activityModel];
//            model.title = _name;
//            [[ActivityDataBaseHandle sharedActivityDataBaseHandle] insertModel:model];
//        }
//        else{
//            UIAlertController *alert2 = [UIAlertController alertControllerWithTitle:@"提示" message:@"该活动已收藏" preferredStyle:UIAlertControllerStyleAlert];
//            UIAlertAction *alertAction2 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];
//            [alert2 addAction:alertAction2];
//            [self presentViewController:alert2 animated:YES completion:^{
//                
//            }];
//        }
//    }
//    else{
//        UIAlertController *alert2 = [UIAlertController alertControllerWithTitle:@"提示" message:@"该活动已收藏" preferredStyle:UIAlertControllerStyleAlert];
//        UIAlertAction *alertAction2 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];
//        [alert2 addAction:alertAction2];
//        [self presentViewController:alert2 animated:YES completion:^{
//            
//        }];
//    }
//
//}

- (void) itemAction2:(UIBarButtonItem *) item {
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
